package com.vogue.supplymall.shop.domain.dao.impl;

import com.vogue.supplymall.common.domain.dto.RelevanceInfo;
import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.shop.domain.dao.RelevanceDao;
import com.vogue.supplymall.shop.domain.mapper.RelevanceMapper;
import com.vogue.supplymall.shop.domain.model.Relevance;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Relevance dao.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-03-28
 */
@Repository("relevanceDao")
public class RelevanceDaoImpl extends BaseDaoImpl<Relevance, Long> implements RelevanceDao {
	/**
	 * The Relevance mapper.
	 */
	@Resource
	private RelevanceMapper relevanceMapper;

	@Override
	public List<Relevance> findRelevance(RelevanceInfo relevanceInfo) {
		Map<String, Object> map = new HashMap<>();
		map.put("type", relevanceInfo.getType());
		map.put("userId", relevanceInfo.getUserId());
		map.put("targetObjType", relevanceInfo.getTargetObjType());
		map.put("targetObjId", relevanceInfo.getTargetObjId());
		return this.selectByWhr0(
				"disabledflag = 0 and type = #{type} and originobjid = #{userId} and targetobjtype = #{targetObjType}  and targetobjid = #{targetObjId}",
				map);
		// relevanceMapper.findRelevance(relevanceInfo);
	}

	@Override
	public List<Map<String, Object>> findProductWatch(SearchInfo searchInfo) {
		return relevanceMapper.findProductWatch(searchInfo);
	}

	@Override
	public List<Map<String, Object>> findShopWatch(SearchInfo searchInfo) {
		return relevanceMapper.findShopWatch(searchInfo);
	}

	@Override
	public Map<String, Object> countWatch(RelevanceInfo relevanceInfo) {
		Map<String, Object> map = new HashMap<>();
		map.put("originobjid", relevanceInfo.getUserId());
		List<Map<String, Object>> list = this.query0(
				"select COUNT(id) watch_count from relevance where originobjid = #{originobjid} and type= 1 and disabledflag = 0 and targetobjtype = 2",
				map);
		if(list == null) {
			map = new HashMap<String, Object>();
			map.put("watch_count", 0);
			return map;
		}else {
			return list.get(0);
		}
	}

	@Override
	public Map<String, Object> countCollect(RelevanceInfo relevanceInfo) {
		Map<String, Object> map = new HashMap<>();
		map.put("originobjid", relevanceInfo.getUserId());
		List<Map<String, Object>> list = this.query0(
				"select COUNT(id) collect_count from relevance where originobjid = #{originobjid} and type= 1 and disabledflag = 0 and targetobjtype = 3",
				map);
		if(list == null) {
			map = new HashMap<String, Object>();
			map.put("collect_count", 0);
			return map;
		}else {
			return list.get(0);
		}
	}
}
